package fs.convert 
{
	/**
	 * !!未完成
	 * @author HS
	 */
	public class Number2CC
	{
		
		public static function convert(n:Number):String
		{
			var cc:String = "";
			var crntN = n;
			if (n < 0)
			{
				cc += "负";
				n = -n;
			}
			cc += convertUint(Math.floor(n));
			cc += convertDecimal(n - Math.floor(n));
			return fix(cc);
		}
		
		static private function fix(str:String):String
		{
			var res:String = "";
			var prefix:String;
			var suffix:String = (str.split("点")[1] == null)?"":"点"+str.split("点")[1];
			str = str.split("点")[0];
			if (str.charAt(0) == "负")
			{
				res = str.substring(1);
				prefix = "负";
			}
			else
			{
				res = str;
				prefix = "";
			}
			//修正开头的零
			var pt:uint = 0;
			while (pt<res.length&&res.charAt(pt) == "零")
			{
				pt++;
			}
			if (pt == res.length)
			{
				return "零";
			}
			else if (res.charAt(pt) == "点")
			{
				res = res.substr(pt - 1);
			}
			else
			{
				res = res.substr(pt);
			}
			//修正连续的零
			var pattern:RegExp=/零+/;
			res.replace(pattern, "零");
			//修正整数尾的零
			//修正开头的一十
			return prefix+res+suffix;
		}
		private static function convertUint(n:uint):String
		{
			if (n > Math.pow(10, 8))
			{
				return convertUint(n / Math.pow(10, 8)) + "亿"+convertUint(n % Math.pow(10, 8));
			}
			else if (n > Math.pow(10, 4))
			{
				return convertUint(n / Math.pow(10, 4)) + "万"+convertUint(n % Math.pow(10, 4));
			}
			else 
			{
				var str:String = "";
				str += convertDigit(Math.floor(n / 1000) + "") ;
				if (Math.floor(n / 1000) != 0)
				{
					str+= "千" ;
				}
				n = n - Math.floor(n / 1000) * 1000;
				str += convertDigit(Math.floor(n / 100) + "") ;
				if (Math.floor(n / 100) != 0)
				{
					str+= "百" ;
				}
				n = n - Math.floor(n / 100) * 100;
				str += convertDigit(Math.floor(n / 10) + "");
				if (Math.floor(n / 10) != 0)
				{
					str+= "十" ;
				}
				n = n - Math.floor(n / 10) * 10;
				if (n != 0)
				{
					str += convertDigit(n+"") ;	
				}
				
				return str;
			}
		}
		private static function convertDecimal(n:Number):String
		{
			if (n == 0) return "";
			var str:String = n + "";
			str = str.split(".")[1];
			var strDec:String = "";
			for (var index = 0; index < str.length;index++ )
			{
				strDec += convertDigit(str.charAt(index));
			}
			return "点"+strDec;
		}
		private static function convertDigit(d:String):String
		{
			switch(d)
			{
				case "0":
				return "零";
				case "1":
				return "一";
				case "2":
				return "二";
				case "3":
				return "三";
				case "4":
				return "四";
				case "5":
				return "五";
				case "6":
				return "六";
				case "7":
				return "七";
				case "8":
				return "八";
				case "9":
				return "九";	
				default:
				return "出错";
			}
		}
		public static function test():void
		{
			trace(Number2CC.convert(0));
			trace(Number2CC.convert(1));
			trace(Number2CC.convert(12));
			trace(Number2CC.convert(123));
			trace(Number2CC.convert(1234));
			trace(Number2CC.convert(12345));
			trace(Number2CC.convert(123456));
			trace(Number2CC.convert(1234567890));
			trace(Number2CC.convert(1000000000));
			trace(Number2CC.convert(1000000005));
			trace(Number2CC.convert(12.034));
			trace(Number2CC.convert(0.034));
			trace(Number2CC.convert(-0.034));
		}
	}

}